package com.thinkingstar.iads.projectcenter.serviceImpl;

import java.util.List;
import java.util.Map;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentEquipInfo;
import com.thinkingstar.iads.datacenter.entity.dao.DaoDcEquipmentEquip;
import com.thinkingstar.iads.datacenter.entity.dao.DaoDcEquipmentEquipInfo;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquip;
import com.thinkingstar.iads.projectcenter.entity.PcProjectEquipmentEquipInfo;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcEquipmentEquip;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectData;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectEquipmentEquipInfo;

/**
 * 项目初始化操作相关
 * @author Administrator
 *
 */
@IocBean
public class ProjectInitServiceImpl {
	
	@Inject
	private DaoPcProjectData daoPcProjectData;
	
	@Inject
	private DaoDcEquipmentEquip daoDcEquipmentEquip;
	
	@Inject 
	private DaoPcEquipmentEquip daoPcEquipmentEquip;
	
	@Inject
	private DaoDcEquipmentEquipInfo daoDcEquipmentEquipInfo;
	
	@Inject
	private DaoPcProjectEquipmentEquipInfo daoPcProjectEquipmentEquipInfo;
	
	
	public void initEquipmentRelated(String project_id){
		NutMap param_map = new NutMap();
		PcProjectInfo pcProjectInfo = daoPcProjectData.fetch(project_id);
		param_map.put("design_phase_id", pcProjectInfo.getProject_design_phase_id());
		param_map.put("design_content_id", pcProjectInfo.getProject_design_content_id());
		//根据项目id取用这个项目拥有的设备配备信息
		List <Map> list = (List<Map>) daoDcEquipmentEquip.findEquipmentEquip(param_map).getList();
		for(Map <String , Object> map : list){
			String equipment_equip_code = (String) map.get("equipment_equip_code");
			String equipment_equip_id = (String) map.get("id");
			NutMap find_map = new NutMap("project_id",project_id);
			find_map.put("equipment_equip_code", equipment_equip_code);
			//查询这个项目的设备配备信息
			List <Map> find_list = (List<Map>) daoPcEquipmentEquip.findEquipmentEquipList(find_map).getList();
			if(find_list == null || find_list.isEmpty()){//未初始化
				PcProjectEquipmentEquip obj = new PcProjectEquipmentEquip(map,project_id);
				obj.setCreate_time(CommonDateUtil.getDateTime());
				PcProjectEquipmentEquip object = daoPcEquipmentEquip.insert(obj);
				//查询这个项目将要取用的子设备信息
				List <DcEquipmentEquipInfo> equipment_info_list = daoDcEquipmentEquipInfo.findEquipInfoByEquipId(equipment_equip_id);
				for(DcEquipmentEquipInfo equipment_equip_info : equipment_info_list){
					//判断该设备是否初始化
					String equipment_id = equipment_equip_info.getEquipment_id();
					List <PcProjectEquipmentEquipInfo> result_list = (List<PcProjectEquipmentEquipInfo>) daoPcProjectEquipmentEquipInfo.findListByEquipmentEquipId(new NutMap("equip_id",equipment_id)).getList();
					if(result_list == null || result_list.isEmpty()){
						PcProjectEquipmentEquipInfo equip_object = new PcProjectEquipmentEquipInfo(equipment_equip_info,object.getId());
						equip_object.setCreate_time(CommonDateUtil.getDateTime());
						daoPcProjectEquipmentEquipInfo.insert(equip_object);
					}
				}
			}
		}
		
		
	}
	

}
